<%@ Application Language="C#" %>

<script RunAt="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(Object sender, EventArgs e)
    {
        // Log all unhandled errors
        Utilities.LogError(Server.GetLastError());
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        string cookieName = System.Web.Security.FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        if (authCookie == null)
        {
            return;
        }

        System.Web.Security.FormsAuthenticationTicket authTicket;

        try
        {
            authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
        }
        catch
        {
            return;
        }

        if (authTicket == null)
        {
            return;
        }

        /****** authTicket.UserData is an empty string here on a Web App project *****/
        string[] roles = authTicket.UserData.Split(new char[] { '|' });


        System.Web.Security.FormsIdentity id = new System.Web.Security.FormsIdentity(authTicket);
        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
        Context.User = principal;
    }

       
</script>

